﻿using GN.Pay.TradeChannel.AliPay.Response;
using GN.Pay.Utils.Net.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace GN.Pay.TradeChannel.AliPay.Request
{
    [Serializable]
    class QueryRequest : AServiceRequest
    {
        private ITradeChannel channel;
        private AliPayConfigure config;

        public QueryRequest(ITradeChannel channel,
             AliPayConfigure config,
             string tradeNo,
             string outTradeNo)
        {
            this.channel = channel;
            this.config = config;
            Trade_no = tradeNo;
            Out_trade_no = outTradeNo;
            Partner = config.ConfigureInfo.AccountId;
            Sign_Type = "RSA";
            _Input_Charset = config.Encoding.WebName;
        }

        /// <summary>
        /// 获取服务
        /// </summary>
        public override string Service
        {
            get
            {
                return "single_trade_query";
            }
        }

        /// <summary>
        /// 业务流水号
        /// </summary>
        public string Trade_no { get; private set; }

        /// <summary>
        /// 商户订单号
        /// </summary>
        public string Out_trade_no { get; private set; }

        /// <summary>
        /// 生成
        /// </summary>
        /// <returns></returns>
        public QueryResponse Build()
        {
            var param = this.BuildSignDictionary(channel, config, "Sign", "Sign_Type");
            param = param.Clone(k =>
            {
                return k.ToLower();
            }, v =>
            {
                return HttpUtility.UrlEncode(v, config.Encoding);
            });
            string url = config.GateWayUrl + "?_input_charset=" + config.Encoding.WebName;
            string responseString = HttpUtils.PostString(url, param, config.Encoding);
            return new QueryResponse(responseString, channel, config);
        }
    }
}
